package cms.web.tag;

import cms.bean.forumCode.ForumCodeReference;
import cms.common.data.ApiResultCode;
import cms.common.exception.ServiceException;
import cms.utils.JsonUtils;
import cms.utils.threadLocal.TemplateThreadLocal;
import freemarker.core.Environment;
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.template.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

/**
 * 抽象类
 */
@Slf4j
public abstract class BaseTemplateMethods  implements TemplateDirectiveModel{
    /**
     * @param env freemarker 环境变量
     * @param params   参数
     * @param loopVars 循环变量
     * @param body     指令内容体
     */
    public void execute(Environment env, Map params, TemplateModel[] loopVars,
                        TemplateDirectiveBody body) throws TemplateException, IOException {
        if(log.isInfoEnabled()&&MapUtils.isNotEmpty(params)){
            log.info("params={}",params);
        }
        doExecute(env, params, loopVars, body);
    }

    /**
     * @param env freemarker 环境变量
     * @param params   参数
     * @param loopVars 循环变量
     * @param body     指令内容体
     */
    protected abstract void doExecute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException ;

    protected BeansWrapper getBeansWrapper() {
        //创建builder:
        BeansWrapperBuilder builder = new BeansWrapperBuilder(Configuration.getVersion());
        // 设置所需的beanswrapper属性
        //builder.setUseModelCache(true);是否启用缓存
        //builder.setExposeFields(true);//是否启用返回类的公共实例。
        builder.setSimpleMapWrapper(true);//模板能使用Map方法
        // Get the singleton:
        BeansWrapper beansWrapper = builder.build();

        return beansWrapper;
    }



    /**
     * 解析自定义标签的属性标签
     *
     * @param params 标签参数
     * @param label  属性标签
     * @return
     */
    protected ForumCodeReference forumCodeReferenceAction(Map params, String label) throws TemplateModelException {
        ForumCodeReference forumCodeReference = null;
        if (MapUtils.isNotEmpty(params)) {
            Iterator paramIter = params.entrySet().iterator();
            while (paramIter.hasNext()) {
                Map.Entry ent = (Map.Entry) paramIter.next();
                String key = (ent.getKey() + "").trim();
                if (StringUtils.isNotBlank(key)) {
                    if (StringUtils.equalsIgnoreCase(label, key)) {
                        String value = (ent.getValue() + "").trim();
                        if(StringUtils.isBlank(value)){
                            log.error("解析自定义标签的属性{}异常",key);
                            throw new TemplateModelException("缺少必须参数"+label+"错误!");
                        }
                        forumCodeReference = ForumCodeReference.builder(value);
                        paramIter.remove();
                        break;
//							String[] anctions = StringUtils.split(value,"|");//按|号分割
//							referenceCode = anctions[0];
//							modulePath = anctions[1];
                    }
                }
            }
        }
        return forumCodeReference;
    }
}
